;序号+ xuzq  是需要添加的流程
;版本号为1.1.0
;Installer_XXX.exe 修改卸载程序名
;默认的安装器名为inst.ep，Installer_XXX.exe应修改为inst.ep
;默认的输出卸载文件名为inst.ep，WriteUninstaller $INSTDIR\uninst.ep
;界面卸载程序需要签名

;
;可以配置uninst.ini修改默认文件名
;UnInstall/InstallName修改安装器序名
;UnInstall/UninstallName修改输出卸载文件名

Unicode true

;参数解析必须包含这个头文件
!include "FileFunc.nsh"

;1.  xuzq 需要添加的头文件和插件
!include "LogicLib.nsh"
!include "StdUtils.nsh"

;2.  xuzq 添加控制台头文件
!include ".\console.nsh"

!include "x64.nsh"
; 安装程序初始定义常量
; !define PRODUCT_NAME 迅捷视频转换器
; !define URL_NAME VideoConverter
; !define INSTALL_DIR VideoConverter
; !define EXE_NAME VideoConverter.exe
; !define ICONFILE logo.ico
; !define PRODUCT_VERSION 2.3.4.0
; !define PRODUCT_PUBLISHER 上海互盾信息科技有限公司
; !define PRODUCT_WEB_SITE http://www.xunjieshipin.com

!define INSTDIRNAME ${INSTALL_DIR}

!define PRODUCT_DIR_REGKEY "Software\Microsoft\Windows\CurrentVersion\App Paths\${EXE_NAME}"
!define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
!define PRODUCT_UNINST_ROOT_KEY HKLM

;安装包文件版本声明
VIAddVersionKey ProductName "${PRODUCT_NAME}" ;产品名称 
VIAddVersionKey ProductVersion "${PRODUCT_VERSION}" ;产品版本 
VIAddVersionKey Comments "${PRODUCT_NAME}" ;备注 
VIAddVersionKey CompanyName "${PRODUCT_PUBLISHER}" ;公司名 
VIAddVersionKey LegalCopyright "版权所有 (C) ${PRODUCT_PUBLISHER}" ;合法版权 
VIAddVersionKey InternalName "${URL_NAME}" ;内部名称 
; VIAddVersionKey LegalTrademarks "${PRODUCT_PUBLISHER}" ;合法商标 ; 
; VIAddVersionKey OriginalFilename "XX.exe" ;源文件名 
; VIAddVersionKey PrivateBuild "XX" ;个人内部版本说明 
; VIAddVersionKey SpecialBuild "XX" ;特殊内部本本说明
VIProductVersion "${PRODUCT_VERSION}" ;版本号，格式为 X.X.X.X (若使用则本条必须) 
VIAddVersionKey FileDescription "${PRODUCT_NAME}" ;文件描述(标准信息) 
VIAddVersionKey FileVersion "${PRODUCT_VERSION}" ;文件版本(标准信息) 

SetCompressor lzma
Name ${PRODUCT_NAME} ${PRODUCT_VERSION}
OutFile setupfiles\setup_${PRODUCT_NAME}_${PRODUCT_VERSION}_${SYSTEM_TYPE}.exe
InstallDir $PROGRAMFILES\${PRODUCT_NAME}\${URL_NAME}
Icon ${ICONFILE}
UninstallIcon "${NSISDIR}\Contrib\Graphics\Icons\modern-uninstall.ico"
SilentInstall silent
SilentUninstall silent
;ShowInstDetails show
;ShowUninstDetails show
BrandingText ${PRODUCT_NAME}
RequestExecutionLevel admin

;3.  xuzq 添加解析安装参数函数
Var /GLOBAL giDsc
Var /GLOBAL giSsc
Var /GLOBAL giConsole
Var /GLOBAL giInstdir
Var /GLOBAL giInstalldir
Var /GLOBAL giUpdate

Function GetInstCmdParam
	${GetParameters} $R0	
	${GetOptions} $R0 "-dsc=" $R1
	StrCpy  $giDsc $R1
	${GetOptions} $R0 "-ssc=" $R1
	StrCpy  $giSsc $R1
	${GetOptions} $R0 "-console=" $R1
	StrCpy  $giConsole $R1
	${GetOptions} $R0 "-instdir=" $R1
	StrCpy  $giInstdir $R1
  ;xuzq V1.2.0
    ${GetOptions} $R0 "-installdir" $R1
	${If} $R1 != ""
		StrCpy  $giInstalldir "$R1\${URL_NAME}"
	${EndIf}
	;自动更新模块
	${GetOptions} $R0 "-update=" $R1
	StrCpy  $giUpdate $R1
	
FunctionEnd

Function GetNetFrameworkVersion
    ;获取.Net Framework版本支持
    Push $1
    Push $0
    ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full" "Install"
    ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full" "Version"
    StrCmp $0 1 KnowNetFrameworkVersion +1
    ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5" "Install"
    ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5" "Version"
    StrCmp $0 1 KnowNetFrameworkVersion +1
    ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup" "InstallSuccess"
    ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup" "Version"
    StrCmp $0 1 KnowNetFrameworkVersion +1
    ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v2.0.50727" "Install"
    ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v2.0.50727" "Version"
    StrCmp $1 "" +1 +2
    StrCpy $1 "2.0.50727.832"
    StrCmp $0 1 KnowNetFrameworkVersion +1
    ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v1.1.4322" "Install"
    ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v1.1.4322" "Version"
    StrCmp $1 "" +1 +2
    StrCpy $1 "1.1.4322.573"
    StrCmp $0 1 KnowNetFrameworkVersion +1
    ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\.NETFramework\policy\v1.0" "Install"
    ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\.NETFramework\policy\v1.0" "Version"
    StrCmp $1 "" +1 +2
    StrCpy $1 "1.0.3705.0"
    StrCmp $0 1 KnowNetFrameworkVersion +1
    StrCpy $1 "not .NetFramework"
    KnowNetFrameworkVersion:
    Pop $0
    Exch $1
FunctionEnd

Function .onInit
  SetOverwrite on
  

  
;4. xuzq 调用安装参数函数
  Call GetInstCmdParam
;5. xuzq 初始化控制台
  ${If} $giConsole == 'show'
	  ${INIT_CONSOLE}
  ${EndIf}

;6.  xuzq 安装版本号 三段式1.0.0
${WRITE_LINE_CONSOLE}  "InstallVersion=1.2.0"
;7.  xuzq 开始输出进度
  ${WRITE_LINE_CONSOLE} "Progress=0.0"

FunctionEnd

Function .onInstSuccess

FunctionEnd

Section MainSection SEC01

  ${If} $giInstdir != ""
	StrCpy $INSTDIR "$giInstdir"
  ${ElseIf} $giInstalldir != ""
	StrCpy $INSTDIR "$giInstalldir"
  ${Else}
	ReadRegStr $0 HKLM "${PRODUCT_DIR_REGKEY}" ""
	${If} $0 != ""
		${StdUtils.GetParentPath} $1 "$0"
		StrCpy  $INSTDIR   $1
	${EndIf}
  ${EndIf}

	SetOutPath $INSTDIR
  
;8.  xuzq 分段抽取文件，输出进度
${WRITE_LINE_CONSOLE} "Progress=0.1"
  ;File /r "Release_lianxiang\CopyRight"
  ;File /r "Release_lianxiang\*.ini"
  ${WRITE_LINE_CONSOLE} "Progress=0.2"
  File /r "Release_lianxiang\*.dll"
    ${WRITE_LINE_CONSOLE} "Progress=0.3"
  ;File /r "Release_lianxiang\locales"
  File /r "Release_lianxiang\resources"
  File /r "Release_lianxiang\*.pak"
    ${WRITE_LINE_CONSOLE} "Progress=0.4"
  File /r "Release_lianxiang\*.data"
    ${WRITE_LINE_CONSOLE} "Progress=0.5"
  File /r "Release_lianxiang\*.bin"
  File /r "Release_lianxiang\*.html"
  File /r "Release_lianxiang\version"
  File /r "Release_lianxiang\LICENSE"
  File /r "Release_lianxiang\*.json"
  ;File /r "Release_lianxiang\commonTool"
    ${WRITE_LINE_CONSOLE} "Progress=0.68"
  File /r "Release_lianxiang\*.dat"
  ;File /r "Release_lianxiang\logo.ico"

  ${WRITE_LINE_CONSOLE} "Progress=0.74"
  ;File /r "Release_lianxiang\translations"
  ${WRITE_LINE_CONSOLE} "Progress=0.89"
  File /r "Release_lianxiang\*.exe"
  ${WRITE_LINE_CONSOLE} "Progress=1.0"		
;8.  xuzq 输出可执行文件 和 卸载程序
  ${WRITE_LINE_CONSOLE} "ExePath=$INSTDIR\${EXE_NAME}"
  ${WRITE_LINE_CONSOLE} "UninstExePath=$INSTDIR\uninst.exe"
SectionEnd
  
;10.  xuzq 快捷启动函数
Function CreateBarlnk
  ReadRegStr $R0 HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion" "CurrentVersion"
  ${if} $R0 >= 6.0
    ${StdUtils.InvokeShellVerb} $0 "$INSTDIR" "${EXE_NAME}" ${StdUtils.Const.ShellVerb.PinToTaskbar}
  ${else} 
    CreateShortCut "$QUICKLAUNCH\${PRODUCT_NAME}.lnk" "$INSTDIR\${EXE_NAME}"
  ${Endif}
FunctionEnd

Section -AdditionalIcons
  WriteRegStr HKLM "${PRODUCT_DIR_REGKEY}" "" "$INSTDIR\${EXE_NAME}"
;11.  xuzq;设置所有用户环境 当设置所有用户时需要
  SetShellVarContext all
  #WriteINIStr "$INSTDIR\config" "Install" "SetupName" $EXEFILE
  #WriteINIStr "$INSTDIR\config" "Install" "Version" ${PRODUCT_VERSION}
  
  
;12.  xuzq 创建开始菜单栏启动方式
	;CreateDirectory $INSTDIR\wkeStorage
	CreateDirectory $SMPROGRAMS\${PRODUCT_NAME}
	CreateShortCut $SMPROGRAMS\${PRODUCT_NAME}\${PRODUCT_NAME}.lnk $INSTDIR\${EXE_NAME}

;13.  xuzq 创建开始菜单栏启动方式 卸载
	CreateShortCut "$SMPROGRAMS\${PRODUCT_NAME}\卸载 ${PRODUCT_NAME}.lnk" $INSTDIR\uninst.exe
  
;14.  xuzq 桌面快捷方式
	${If} $giDsc != 'false'
  	CreateShortCut $DESKTOP\${PRODUCT_NAME}.lnk $INSTDIR\${EXE_NAME}
	${EndIf}

;15.  xuzq快捷启动
	${If} $giSsc == 'true'
	  Call CreateBarlnk
	${EndIf}
  ;16. V1.2.0  
  ;${If} $giInstdir == ""
    ;实际生成的卸载文件修改为uninst.ep /* 用普通用户权限运行主程序，此处若编译失败，需要安装对应的拓展 */  
   ExecWait '"$INSTDIR\${EXE_NAME}" "-install"'
   ;ShellExecAsUser::ShellExecAsUser "open" "$INSTDIR\${EXE_NAME}"
;${If} $giUpdate == ""
    /* 打开欢迎网页 */
    ;ExecShell "open" "http://tj.sjhfrj.com/redirect/ver1/${URL_NAME}/help/${PRODUCT_VERSION}/installer/123456"
;${else}

 ;${EndIf}
SectionEnd

Section -Post
;17.  实际生成的卸载文件修改为uninst.ep
  WriteUninstaller $INSTDIR\uninst.data
;18.  卸载程序
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} ${PRODUCT_UNINST_KEY} UninstallString $INSTDIR\uninst.exe

  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} ${PRODUCT_UNINST_KEY} DisplayName $(^Name)
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} ${PRODUCT_UNINST_KEY} DisplayVersion ${PRODUCT_VERSION}
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} ${PRODUCT_UNINST_KEY} URLInfoAbout ${PRODUCT_WEB_SITE}
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} ${PRODUCT_UNINST_KEY} Publisher ${PRODUCT_PUBLISHER}
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} ${PRODUCT_UNINST_KEY} DisplayIcon $INSTDIR\${EXE_NAME}
SectionEnd
/******************************
 *  以下是安装程序的卸载部分  *
 ******************************/

;1.  xuzq;卸载获取参数函数
Var /GLOBAL guConsole
Function un.GetUnInstCmdParam
	${GetParameters} $R0
	${GetOptions} $R0 "-console=" $R1
	StrCpy  $guConsole $R1
	DetailPrint "console=$guConsole"
FunctionEnd
Function un.onInit
;2  xuzq;调用获取参数函数
  Call un.GetUnInstCmdParam
;3.  xuzq;初始化控制台
  ${If} $guConsole == 'show'
    ${INIT_CONSOLE}
  ${EndIf}
;4  xuzq 安装版本号 三段式1.0.0
${WRITE_LINE_CONSOLE}  "InstallVersion=1.2.0"
;5  xuzq;开始输出进度
${WRITE_LINE_CONSOLE} "Progress=0.0"


FunctionEnd

;6.  xuzq;卸载时删除快捷方式函数
Function un.DelBarlnk
  ReadRegStr $R0 HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion" "CurrentVersion"  
  ${if} $R0 >= 6.0  
    ${StdUtils.InvokeShellVerb} $0 "$INSTDIR" "${EXE_NAME}" ${StdUtils.Const.ShellVerb.UnpinFromTaskbar}
  ${else}  
    delete "$QUICKLAUNCH\${PRODUCT_NAME}.lnk"  
  ${Endif}  
FunctionEnd  


Section Uninstall

FindProcDLL::FindProc "${EXE_NAME}"
  Pop $R0
  IntCmp $R0 1 0 no_run
  ${WRITE_LINE_CONSOLE} "Error=安装程序检测到$(^Name)正在运行，请退出程序后重试"
  Quit
  no_run:

;7.  xuzq;设置所有用户环境 当设置所有用户时需要
	SetShellVarContext all
	
  ;ExecWait '"$INSTDIR\${EXE_NAME}" "-uninstall"' 
  
;8.  xuzq;调用删除快捷方式
  Call  un.DelBarlnk

;9.  xuzq; 卸载程序并调用删除进度
  ${WRITE_LINE_CONSOLE} "Progress=0.0"
  Delete "$DESKTOP\${PRODUCT_NAME}.lnk"
  Delete "$SMPROGRAMS\${PRODUCT_NAME}\卸载 ${PRODUCT_NAME}.lnk"
  Delete "$SMPROGRAMS\${PRODUCT_NAME}\${PRODUCT_NAME}.lnk"
  RMDir  "$SMPROGRAMS\${PRODUCT_NAME}"
  ${WRITE_LINE_CONSOLE} "Progress=0.1"
  
  Delete "$DESKTOP\${PRODUCT_NAME}.lnk"
  Delete "$SMPROGRAMS\${PRODUCT_NAME}\卸载 ${PRODUCT_NAME}.lnk"
  Delete "$SMPROGRAMS\${PRODUCT_NAME}\${PRODUCT_NAME}.lnk"
  RMDir  "$SMPROGRAMS\${PRODUCT_NAME}"

  DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}"
  DeleteRegKey HKLM "${PRODUCT_DIR_REGKEY}"

  ${WRITE_LINE_CONSOLE} "Progress=0.2"
  ;nsExec::Exec "$INSTDIR\${EXE_NAME} -uninstall"

  ;Delete "$INSTDIR\bearer"
  ;${WRITE_LINE_CONSOLE} "Progress=0.3"
  ;Delete "$INSTDIR\iconengines"
  ;${WRITE_LINE_CONSOLE} "Progress=0.4"
  ;Delete "$INSTDIR\imageformats"
  ;${WRITE_LINE_CONSOLE} "Progress=0.5"
  ;Delete "$INSTDIR\platforms"
  ;${WRITE_LINE_CONSOLE} "Progress=0.6"
  ;Delete "$INSTDIR\styles"
  ;${WRITE_LINE_CONSOLE} "Progress=0.7"
  ;Delete "$INSTDIR\translations"
  ;${WRITE_LINE_CONSOLE} "Progress=0.8"
  ;Delete "$INSTDIR\*.*"
  ;${WRITE_LINE_CONSOLE} "Progress=0.9"
  ;Delete "$INSTDIR\*"
  ;${WRITE_LINE_CONSOLE} "Progress=0.98"
  Delete "$INSTDIR\*.*"
  ${WRITE_LINE_CONSOLE} "Progress=0.6"
  Delete "$INSTDIR\*"
  ${WRITE_LINE_CONSOLE} "Progress=0.8"
  RMDir /R "$INSTDIR"
  RMDir /r /REBOOTOK "$INSTDIR"
   ${WRITE_LINE_CONSOLE} "Progress=1.00"
  ; SetAutoClose true
SectionEnd

Function un.onUninstSuccess
  ; MessageBox MB_ICONINFORMATION|MB_OK "$(^Name) 已成功地从您的计算机移除。"
FunctionEnd
